package com.dd.cloud.user.service.admin.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.constants.FeedbackConstants;
import com.dd.cloud.common.constants.UserConstants;
import com.dd.cloud.user.entity.admin.Feedback;
import com.dd.cloud.user.entity.doctor.DoctorDetail;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.user.mapper.admin.FeedbackMapper;
import com.dd.cloud.user.req.admin.FeedbackPageReq;
import com.dd.cloud.user.res.admin.FeedbackDocPageRes;
import com.dd.cloud.user.res.admin.FeedbackPageRes;
import com.dd.cloud.user.service.admin.IFeedbackReplyService;
import com.dd.cloud.user.service.admin.IFeedbackService;
import com.dd.cloud.user.service.doctor.IDoctorDetailService;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.common.enums.ZkExceptionEnum;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.common.utils.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author czg
 * @since 2022-04-27
 */
@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements IFeedbackService {
    @Autowired
    private IDoctorDetailService doctorDetailService;
    @Autowired
    private IFeedbackReplyService feedbackReplyService;

    /**
     * 添加反馈内容
     *
     * @param feedback
     */
    @Override
    public void addFeedback(Feedback feedback) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo().get();
        if (loginInfo.getType() == LoginInfo.UserType.DOCTOR) {
            QueryWrapper<DoctorDetail> qw = new QueryWrapper<>();
            qw.lambda().eq(DoctorDetail::getDoctorId, loginInfo.getId())
                    .eq(DoctorDetail::getStatus, UserConstants.DoctorDetailStatus.NORMAL);
            DoctorDetail doctorDetail = doctorDetailService.getOne(qw);
            if (doctorDetail != null) {
                feedback.setName(doctorDetail.getName());
            }
        }
        feedback.setUserId(loginInfo.getId());
        feedback.setUserType(loginInfo.getType());
        feedback.setStatus(FeedbackConstants.FeedbackStatus.NOMER);
        feedback.setCreateDate(LocalDateTime.now());
        boolean i = save(feedback);
        if (!i) {
            throw new ZekeException(ZkExceptionEnum.ADD_ERROR);
        }
    }

    /**
     * 医生查询反馈列表
     *
     * @param req
     * @return
     */
    @Override
    public Page<FeedbackDocPageRes> getFeedbackPage(FeedbackPageReq req) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo(LoginInfo.UserType.DOCTOR).get();
        Page<FeedbackDocPageRes> page = req.pagePojo();
        req.setDoctorId(loginInfo.getId());
        page = getBaseMapper().getFeedbackDocPage(page, req);
        return page;
    }

    /**
     * 管理员查询问题反馈列表
     *
     * @param req
     * @return
     */
    @Override
    public IPage<FeedbackPageRes> getFeedbackPageByAdmin(FeedbackPageReq req) {
        Page<FeedbackPageRes> page = req.pagePojo();
        page = getBaseMapper().getFeedbackPage(page, req);
        return page;
    }

    /**
     * 修改反馈状态
     *
     * @param id
     * @param status
     */
    @Override
    public void updateFeedbackStatus(Integer id, Integer status) {
        Feedback feedback = getById(id);
        if (feedback == null) {
            throw new ZekeException(400, "反馈不存在");
        }
        if (feedback.getStatus().equals(status)) {
            return;
        }
        if (!ObjectUtils.refrect(FeedbackConstants.FeedbackStatus.class, status)) {
            throw new ZekeException(ZkExceptionEnum.STATUS_DATA_ERROR);
        }
        feedback.setStatus(status);
        boolean i = updateById(feedback);
        if (!i) {
            throw new ZekeException(ZkExceptionEnum.REVIEW_ERROR);
        }
    }


}
